<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>角色管理</title>
		<link href="../../component/pear/css/pear.css" rel="stylesheet" />
	</head>
	<body class="pear-container">
	<!--信息展示表格-->
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="role-table" lay-filter="role-table"></table>
			</div>
		</div>
		<!--新增/批量删除组件-->
		<script type="text/html" id="role-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
				<i class="layui-icon layui-icon-add-1"></i>
				新增
			</button>
			<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
				<i class="layui-icon layui-icon-delete"></i>
				删除
			</button>
		</script>
		<!--修改/删除组件-->
		<script type="text/html" id="role-bar">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
			<button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
		</script>
	<!--新增/修改表单组件-->
	<div style="display: none" id="showRole">
		<form class="layui-form" action="" lay-filter="roleForm">
			<input type="hidden" name="aid">
			<div class="mainBox">
				<div class="main-container">
					<div class="main-container">
						<div class="layui-form-item">
							<label class="layui-form-label">角色</label>
							<div class="layui-input-block">
								<input type="text" name="role" lay-verify="required|role" autocomplete="off" placeholder="请输入角色名" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">描述</label>
							<div class="layui-input-block">
								<input type="text"  name="info" lay-verify="title" autocomplete="off" placeholder="请输入描述" class="layui-input">
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="bottom">
				<div class="button-container">
					<button type="button" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="role-submit">
						<i class="layui-icon layui-icon-ok"></i>
						提交
					</button>
					<button type="reset" class="pear-btn pear-btn-sm">
						<i class="layui-icon layui-icon-refresh"></i>
						重置
					</button>
				</div>
			</div>
		</form>
	</div>

		<script src="../../component/layui/layui.js"></script>
		<script src="../../component/pear/pear.js"></script>
		<script src="../../component/js/role.js"></script>
		<script>
		    layui.use(['table','form','jquery'],function () {
		        let table = layui.table;
		        let form = layui.form;
		        let $ = layui.jquery;
		        let MODULE_PATH = "operate/";
		        let cols = [
		            [
		                {type:'checkbox'},
						{title:'序号',field: 'aid',align: 'center'},
		                {title: '角色名', field: 'role', align:'center', width:100},
		                {title: '描述', field: 'info', align:'center'},
		                {title: '操作', toolbar: '#role-bar', align:'center', width:195}
		            ]
		        ]
		        table.render({
		            elem: '#role-table',
		            url: 'http://localhost:8080/auths/selectAll1',
		            page: true,
					limit:5,
					limits:[5,10,15,20],
		            cols: cols ,
		            skin: 'line',
		            toolbar: '#role-toolbar',
		            defaultToolbar: [{
						title: '刷新',
		                layEvent: 'refresh',
		                icon: 'layui-icon-refresh',
		            }, 'filter', 'print', 'exports']
		        });

		        table.on('tool(role-table)', function(obj){
		            if(obj.event === 'remove'){
		                window.remove(obj);
		            } else if(obj.event === 'edit'){
		                window.edit(obj);
		            } else if(obj.event === 'power'){
		                window.power(obj);
		            }
		        });

		        table.on('toolbar(role-table)', function(obj){
		            if(obj.event === 'add'){
		                window.add();
		            } else if(obj.event === 'refresh'){
		                window.refresh();
		            } else if(obj.event === 'batchRemove'){
		                window.batchRemove(obj);
		            }
		        });
		        let index = 0;
		        window.add = function(){
                    index = layer.open({
                        type: 1,
                        title: '新增权限',
                        area: ['500px', '300px'],
                        content: $('#showRole')
                    });
                    form.on('submit(role-submit)', function(data){
						let loading = layer.load(2)
                        $.post(
                            'http://localhost:8080/auths/insertAuths',
							data.field,
								function (msg) {//回调函数
									layer.close(index)
                                    table.reload('role-table')
									layer.close(loading)
									layer.msg('添加成功',{icon: 6,time: 2000})
                                }
						)
						return false;
                    });
		        }
		        window.edit = function(obj){
		        	let loading = layer.load(2);
		            //获取回显数据
		            $.get(
		                //url
		                'http://localhost:8080/auths/selectOne',
                        //参数
                        {aid:obj.data.aid},
                        //行为（对数据的操作）
                        function (data) {
		                    //打开弹窗
                            index = layer.open({
                                type: 1,
                                title: '修改权限',
                                area: ['500px', '300px'],
                                //打开id为showRole的弹出页面
                                content: $('#showRole'),
                            }),
                            //给表单赋值
                            form.val('roleForm', data)
							layer.close(loading)
                        }
                    )
                    //监听表单提交
                    form.on('submit(role-submit)', function(data){
                    	let loading = layer.load()
                        $.post(
                            //url
                            'http://localhost:8080/auths/updateAuths',
                            //参数
                            data.field,//field指对象所有属性
                            function (msg) {
                                //关闭弹窗
                                layer.close(index)
								//刷新表格
								table.reload('role-table')
                                //弹窗提示信息
                                layer.msg('修改成功',{icon: 6,time: 2000})
                                //关闭加载动画
								layer.close(loading)
							}
                        )
                    });
		        }

		        window.remove = function(obj){
		            layer.confirm('确定要删除该角色', {icon: 3, title:'提示'}, function(index){
		                layer.close(index);
                        let loading = layer.load(2);
		                $.get(
		                    'http://localhost:8080/auths/deleteAuths',
							{aid:obj.data.aid},
							function(msg){
                                table.reload('role-table')
                                layer.close(loading)
                                layer.msg('删除成功',{icon: 6,time: 2000})
							}
						);
		            });
		        }

		        window.batchRemove = function(obj){
					//obj.config.id:根据选中的id获取对象
		            let data = table.checkStatus(obj.config.id).data;
		            console.log(table.checkStatus(obj.config.id))
		            if(data.length === 0){
		                layer.msg("未选中数据",{icon:3,time:1000});
		                return false;
		            }
		            let ids = "";
		            for(let i = 0;i<data.length;i++){
		                ids += data[i].aid+",";
		            }
		            //删除掉ids中的最后一个逗号
		            ids = ids.substr(0,ids.length-1);
		            layer.confirm(
		            		'确定要删除这些用户',
							{icon: 3, title:'提示'},
							function(index){
							layer.close(index);
							let loading = layer.load(2);
							$.get(
								'http://localhost:8080/auths/authsBatchRemove',
								{aids:ids},
								function(msg){
									table.reload("role-table")
									layer.close(loading)
									layer.msg('删除成功',{icon: 6,time: 2000})
								}
							)
		            });
		        }

		        window.refresh = function(){
		            table.reload('role-table');
		        }
		    })
		</script>
	</body>
</html>
